package hut.game.table;

import hut.game.GameActivity;
import hut.game.R;
import hut.game.unit.QuanCo;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;

public class BanCo {
	public static OCo ViTri[][] = new OCo[10][9];
	public static void KhoiTao(Context context,OnTouchListener t) // Constructor khoi tao mang ViTri trong
	{
		int k = 40;
		for (int i = 0; i <= 9; i++) {
			for (int j = 0; j <= 8; j++) {
				BanCo.ViTri[i][j] = new OCo();
				ViTri[i][j].Hang = i;
				ViTri[i][j].Cot = j;
				ViTri[i][j].Trong = true;
				ViTri[i][j].Ten = "";
				ViTri[i][j].ThuTu = "";
				ViTri[i][j].Phe = 0;
				ViTri[i][j].imgMove = new ImageView(context);
				ViTri[i][j].imgMove.setId(k++);
				ViTri[i][j].imgMove.setImageResource(R.drawable.canmove);
				ViTri[i][j].imgMove.setLayoutParams(new LayoutParams(22, 22));
				ViTri[i][j].imgMove.setBackgroundColor(Color.TRANSPARENT);
				GameActivity.setLayout(ViTri[i][j].imgMove, j * 51 + 28, i * 71  + 106,0, 0);
				ViTri[i][j].imgMove.setVisibility(ImageView.INVISIBLE);
				ViTri[i][j].imgMove.setOnTouchListener(t);
			}
		}
	}
	
	public static void ResetCanMove() {
		for (int i = 0; i <= 9; i++) {
			for (int j = 0; j <= 8; j++) {
				BanCo.ViTri[i][j].imgMove.setVisibility(ImageView.INVISIBLE);
			}
		}
	}

	public static void ResetBanCo() {
		for (int i = 0; i <= 9; i++) {
			for (int j = 0; j <= 8; j++) {
				ViTri[i][j].Hang = i;
				ViTri[i][j].Cot = j;
				ViTri[i][j].Trong = true;
				ViTri[i][j].Ten = "";
				ViTri[i][j].ThuTu = "";
				ViTri[i][j].Phe = 0;
				ViTri[i][j].imgMove.setVisibility(ImageView.INVISIBLE);
			}
		}
	}
	//------------------------ Method imgMoveClick 
	public static void imgMoveClick(View sender,int id) {
		for (int i = 0; i <= 9; i++) {
			for (int j = 0; j <= 8; j++) {
				if(sender.equals(BanCo.ViTri[i][j].imgMove))
				{
					if (VanCo.Marked) {
						if (BanCo.ViTri[i][j].Trong) {
							VanCo.DanhDau.drawImage();
							// Bo chon quan co
							VanCo.Marked = false;
							// Ghi thong tin vao gamelog
							VanCo.LuuVaoGameLog(sender, VanCo.DanhDau);
							// Tra lai o co trong
							VanCo.OCoTrong(VanCo.DanhDau.Hang, VanCo.DanhDau.Cot);
							// Dat quan co vao vi tri [i,j] tren ban co
							VanCo.DatQuanCo(sender, VanCo.DanhDau, i, j);
							VanCo.ply++;
							// Thay doi luot di
							Log.d("luotdi", "doi luot di trong ban co 1");
							VanCo.DoiLuotDi();
							// Kiem tra chieu bi
							VanCo.KiemTraChieuBi();
							if (VanCo.winner != 2)
							{
								if(VanCo.state)
								{
									if (VanCo.winner == 0) GameActivity.ThongBaoKetThuc.KhoiTao("Player 1 has winner", 1);
									if (VanCo.winner == 1) GameActivity.ThongBaoKetThuc.KhoiTao("Player 2 has winner", 1);
								}else
								{
									if (VanCo.winner == 0) GameActivity.ThongBaoKetThuc.KhoiTao("Computer has winner", 1);
									if (VanCo.winner == 1) GameActivity.ThongBaoKetThuc.KhoiTao("Player 1 has winner", 1);
								}
								GameActivity.showResult();
								KhoaBanCo();
							}
							// Kiem tra chieu tuong
							VanCo.KiemTraChieuTuong();
							BanCo.ResetCanMove();
						}
						else {
							VanCo.DanhDau.drawImage();
							int phekia = 2;
							if (VanCo.DanhDau.Phe == 0)
								phekia = 1;
							else
								phekia = 0;
							QuanCo temp_c = new QuanCo();
							if(VanCo.state)
							{
								if(VanCo.n1.Phe == phekia)
								{
									for(int k = 0; k < VanCo.n1.dsQuanCo.size(); k++)
									{
										if(BanCo.ViTri[i][j].Ten == VanCo.n1.dsQuanCo.get(k).Ten && BanCo.ViTri[i][j].ThuTu == VanCo.n1.dsQuanCo.get(k).ThuTu )
											temp_c = VanCo.n1.dsQuanCo.get(k);
									}
								}else{
									for(int k = 0; k < VanCo.n2.dsQuanCo.size(); k++)
									{
										if(BanCo.ViTri[i][j].Ten == VanCo.n2.dsQuanCo.get(k).Ten && BanCo.ViTri[i][j].ThuTu == VanCo.n2.dsQuanCo.get(k).ThuTu )
											temp_c = VanCo.n2.dsQuanCo.get(k);
									}
								}
							}else {
								if(VanCo.n1.Phe == phekia)
								{
									for(int k = 0; k < VanCo.n1.dsQuanCo.size(); k++)
									{
										if(BanCo.ViTri[i][j].Ten == VanCo.n1.dsQuanCo.get(k).Ten && BanCo.ViTri[i][j].ThuTu == VanCo.n1.dsQuanCo.get(k).ThuTu )
											temp_c = VanCo.n1.dsQuanCo.get(k);
									}
								}else{
									for(int k = 0; k < VanCo.computer.dsQuanCo.size(); k++)
									{
										if(BanCo.ViTri[i][j].Ten == VanCo.computer.dsQuanCo.get(k).Ten && BanCo.ViTri[i][j].ThuTu == VanCo.computer.dsQuanCo.get(k).ThuTu )
											temp_c = VanCo.computer.dsQuanCo.get(k);
									}
								}
							}
							// Ghi thong tin nuoc di vao gamelog
							VanCo.LuuVaoGameLog(sender, temp_c);
							// Bo chon Quan Co
							VanCo.Marked = false;
							// An quan co cua doi phuong
							VanCo.AnQuanCo(temp_c);
							// Tra lai o co trong
							VanCo.OCoTrong(VanCo.DanhDau.Hang, VanCo.DanhDau.Cot);
							// Thiet lap quan co vao vi tri tren ban co
							VanCo.DatQuanCo(sender, VanCo.DanhDau, i, j);
							VanCo.ply++;
							// Thay doi luot di
							Log.d("luotdi", "doi luot di trong ban co 2");
							VanCo.DoiLuotDi();
							// Kiem tra chieu bi
							VanCo.KiemTraChieuBi();
							if (VanCo.winner != 2)
							{
								if(VanCo.state)
								{
									if (VanCo.winner == 0) GameActivity.ThongBaoKetThuc.KhoiTao("Player 1 has winner", 1);
									if (VanCo.winner == 1) GameActivity.ThongBaoKetThuc.KhoiTao("Player 2 has winner", 1);
								}else
								{
									if (VanCo.winner == 0) GameActivity.ThongBaoKetThuc.KhoiTao("Computer has winner", 1);
									if (VanCo.winner == 1) GameActivity.ThongBaoKetThuc.KhoiTao("Player 1 has winner", 1);
								}
								GameActivity.showResult();
								BanCo.KhoaBanCo();
							}
							// Kiá»ƒm tra chiáº¿u tÆ°á»›ng
							VanCo.KiemTraChieuTuong();
							BanCo.ResetCanMove();
						}
					}
				}
			}
		}
	}
	public static void MoBanCo()
    {
        if (VanCo.LuotDi == 0)
        {
            VanCo.LuotDi = 1;
            VanCo.DoiLuotDi();
        }
        else
        {
            VanCo.LuotDi = 0;
            VanCo.DoiLuotDi();
        }
    }

    public static void KhoaBanCo()
    {
    	VanCo.Finish = true;
    	if(VanCo.state){
    		for(int i = 0; i < VanCo.n1.dsQuanCo.size(); i++)
    		{
    			VanCo.n1.dsQuanCo.get(i).Khoa = true;
    		}
    		for(int i = 0; i < VanCo.n2.dsQuanCo.size(); i++)
    		{
    			VanCo.n2.dsQuanCo.get(i).Khoa = true;
    		}
    	}else{
    		for(int i = 0; i < VanCo.n1.dsQuanCo.size(); i++)
    		{
    			VanCo.n1.dsQuanCo.get(i).Khoa = true;
    		}
    		for(int i = 0; i < VanCo.computer.dsQuanCo.size(); i++)
    		{
    			VanCo.computer.dsQuanCo.get(i).Khoa = true;
    		}
    	}
    }
}
